/*
 * Package il.ac.biu.cs.grossmm.impl.server
 * File XComponent.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package il.ac.biu.cs.grossmm.impl.server;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Element;

import il.ac.biu.cs.grossmm.api.server.Loader;
import il.ac.biu.cs.grossmm.xmlUtil.ElementParser;
import il.ac.biu.cs.grossmm.xmlUtil.Xml;
import il.ac.biu.cs.grossmm.xmlUtil.XmlException;


public class XComponent implements ElementParser  {
	private final Element xLoad;
	private final String parserName;
	private final Element xNameMap;
	
	final Map<String, String> map = 
		new HashMap<String, String>(); 
	
	
	Loader mappedLoader;
	
	public XComponent(Element xResource, Loader baseLoader) throws XmlException {
		xLoad = Xml.getRequiredElement(xResource, "load");
		xNameMap = Xml.getRequiredElement(xResource, "namemap");
		
		String p = xLoad.getAttribute("parser");
		if( p.length()==0 )
			parserName = null;
		else 
			parserName = p;
		
		Xml.parseChldren(xNameMap, "map", this);
		
		mappedLoader = new MappedLoader(baseLoader, map, false);
	}
	
	public void parse(Element e) throws XmlException {
		String local = Xml.getRequiredAttribute(e, "local");
		String global = Xml.getRequiredAttribute(e, "global");
		map.put(local, global);
	}
	
	public Loader getMappedLoader() {
		return mappedLoader;
	}

	public String getResourceProviderName() {
		if( parserName==null )
			return "";
		else
			return parserName;
	}

	public Object getCreationHint() {
		if( parserName==null )
			return xLoad.getTextContent();
		else
			return xLoad;
	}
	
	
	public Map<String, String> getResourceDependencies() {
		return map;
	}
}
